<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class User extends CI_Controller {
    /**
     * 前台登录界面
     * @return [type] [description]
     */
    public function index()
    {
        session_start();
        $this->load->library('authority');
        $this->load->library('common');

        $this->load->helper('captcha');

        $url=@$_GET['url']?@$_GET['url']:'';

 /*       $vals = array(
            'word' => '',
            'img_path' => './captcha/',
            'img_url' => 'http://example.com/captcha/',
            'font_path' => './path/to/fonts/texb.ttf',
            'img_width' => '150',
            'img_height' => 30,
            'expiration' => 7200
            );

        $cap = create_captcha($vals);
        var_dump($cap['image']);*/

        $base=$this->config->item('base_url');
        $this->load->view('land',array('base'=>$base,'url'=>$url));
    }
    /**
     * 前台后台的登录
     * @return [type] [description]
     */
    public function login()
    {
        session_start();
        $sign=true;

        $this->load->library('authority');
        $this->load->library('common');
        $this->load->model('m_user','user',true);

        $url=urldecode(@$_POST['url']?@$_POST['url']:'');
        $user=@$_POST['user'];
        $password=@$_POST['pwd'];
        $classify=@$_POST['classify']?@$_POST['classify']:'';
        $code=@$_POST['code'];

        if(empty($user) || empty($password))
        {
            if(empty($user) && empty($password))
            {
                $sign=false;
                $msg="请输入用户名和密码!";
            }
            else if(empty($user))
            {
                $sign=false;
                $msg="请输入用户名！";
            }else{
                $sign=false;
                $msg="请输入密码！";
            }
        }
        if($sign)
        {
            if(empty($code)){
                $sign=false;
                $msg="请输入验证码！";
            }
            if($sign)
            {
                if($_SESSION['code']!=md5($code))
                {
                    $sign=false;
                    $msg="验证码错误!";
                }
            }
            if($sign)
            {
                //连接数据库
                $user_result=$this->user->login($user,$password,$classify);
                if($user_result){
                    $sign=true;
                    $msg="登陆成功！";

                    $_SESSION['user']=true;
                    if($classify=='1')
                    {
                        $this->load->model("m_teacher",'teacher',true);
                        $_SESSION['id']=$user_result[0]->t_id;//老师id
                        $teacher_result=$this->teacher->teacher_search(array('t_id'=>$user_result[0]->t_id));
                        $_SESSION['no']=$user_result[0]->no;
                        $_SESSION['classify']='1';
                        $_SESSION['name']=$teacher_result[0]->name;//教师姓名
                        $user_update=$this->user->updateUser(array('lastTime'=>time(),'lastIP'=>$_SERVER["REMOTE_ADDR"]),array('t_id'=>$_SESSION['id']),$classify);
                    }
                    else if($classify=='2')
                    {
                        $this->load->model("m_student",'student',true);
                        $_SESSION['id']=$user_result[0]->s_id;//学生id
                        $_SESSION['classify']='2';
                        $stu_result=$this->student->student_search(array('s_id'=>$user_result[0]->s_id));
                        $_SESSION['no']=$user_result[0]->no;
                        $_SESSION['name']=$stu_result[0]->name;//学生姓名
                        $user_update=$this->user->updateUser(array('lastTime'=>time(),'lastIP'=>$_SERVER["REMOTE_ADDR"]),array('s_id'=>$_SESSION['id']),$classify);
                    }
                    else
                    {
                        $_SESSION['no']=$user_result[0]->a_id;
                        $_SESSION['id']=$user_result[0]->a_id;//管理员id
                        $_SESSION['name']=$user;
                        $_SESSION['classify']='0';
                        $user_update=$this->user->updateUser(array('lastTime'=>time(),'lastIP'=>$_SERVER["REMOTE_ADDR"]),array('a_id'=>$_SESSION['id']),$classify);
                    }
                    $_SESSION['classify']=$classify;//用户类别
                }
                else
                {
                    $sign=false;
                    $msg="用户名或密码错误！";
                }
            }
        }
        echo json_encode(array("success"=>$sign,"msg"=>$msg,"url"=>$url,"classify"=>$classify));
    }
    public function logout(){
        session_start();
        unset($_SESSION['name']);
        unset($_SESSION["id"]);
        unset($_SESSION["no"]);
        unset($_SESSION['classify']);

        $this->load->library('common');
        $this->common->redirect("user/index");
    }
}

/* End of file welcome.php */
/* Location: ./application/controllers/welcome.php */